﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshome.p.Controllers
{
    public class DishController : Controller
    {
        [OutputCache(Duration = 1)]
        [HttpGet]
        public ViewResult Index(short id)
        {
            RecipeBoy boy = new RecipeBoy();
            var em = new Models.Recipe
            {
                Entity = boy.GetRecipe(id, true),
                Comment = string.Empty,
                Captcha = string.Empty
            };
            return View(em);
        }
        [OutputCache(Duration = 1)]
        public ViewResult Captchauth()
        {
            return View();
        }
        [fox.Exceptask]
        [HttpPost]
        public ActionResult Index(Models.Recipe rm)
        {
            if (string.IsNullOrEmpty(rm.Captcha))
            {
                ModelState.AddModelError("Captcha", "必须提供验证码，请重试！");
            }
            else if (Session["Captcha"] == null || Session["Captcha"].ToString() != rm.Captcha)
            {
                ModelState.AddModelError("Captcha", "验证码不匹配，请重试！");
            }
            if (string.IsNullOrEmpty(rm.Comment))
            {
                ModelState.AddModelError("Comment", "评论至少要1个字，请重试！");
            }
            else if (fox.ShortextFox.Wordstop().Any(w => rm.Comment.Contains(w.Name)))
            {
                ModelState.AddModelError("Comment", "评论中存在敏感词汇，无法提交！");
            }
            if (ModelState.IsValidField("Comment") && ModelState.IsValidField("Entity") && ModelState.IsValidField("Captcha"))
            {
                CommentBoy boy = new CommentBoy();
                boy.Create(new b.Comment { Tid = default(int), Datime = DateTime.Now, Notes = rm.Comment, Rmx = rm.Entity.Mid });
                return RedirectToAction("Index", new { id = rm.Entity.Mid });
            }
            else
            {
                RecipeBoy boy = new RecipeBoy();
                rm.Entity = boy.GetRecipe(rm.Entity.Mid, true);
                return View(rm);
            }
        }
    }
}
